package com.example.java_chatroom_redis_rabbitmq.controller;

import com.example.java_chatroom_redis_rabbitmq.constants.Constant;
import com.example.java_chatroom_redis_rabbitmq.model.Message;
import com.example.java_chatroom_redis_rabbitmq.model.Result;
import com.example.java_chatroom_redis_rabbitmq.model.User;
import com.example.java_chatroom_redis_rabbitmq.service.MessageService;
import com.example.java_chatroom_redis_rabbitmq.service.TodoService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequestMapping("/message")
@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;

    @Autowired
    private TodoService todoService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping("/getMessageList")
    public Result<List<Message>> getMessageBySessionId(Integer sessionId, HttpServletRequest request) {
        List<Message> ret = new ArrayList<>();
        List<String> messageList = redisTemplate.opsForList().range(Constant.redisMessageKey+sessionId,-100,-1);
        if (!messageList.isEmpty()) {
            for (String message : messageList) {
                try {
                    Message message1 = objectMapper.readValue(message,Message.class);
                    ret.add(message1);
                } catch (JsonProcessingException e) {
                    log.error(e.getMessage());
                }
            }
        }else {
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute("user");
            ret = messageService.getMessageBySessionId(sessionId);
            todoService.deleteTodoMessage(user.getUserId(), sessionId);
            if (ret.isEmpty()) return Result.success(ret);
            List<String> redisList = new ArrayList<>();
            for (Message message : ret) {
                try {
                    redisList.add(objectMapper.writeValueAsString(message));
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
            redisTemplate.opsForList().rightPushAll(Constant.redisMessageKey+sessionId,redisList);
            if (user.getTag().equals("normal")) {
                redisTemplate.expire(Constant.redisMessageKey+sessionId,24,TimeUnit.HOURS);
            }else if (user.getTag().equals("active")) {
                redisTemplate.expire(Constant.redisMessageKey+sessionId,72,TimeUnit.HOURS);
            }else {
                redisTemplate.expire(Constant.redisMessageKey+sessionId,120, TimeUnit.SECONDS);
            }
        }

        return Result.success(ret);
    }
}
